<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 敏感字段加密（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个由多个命令字组成的命令字符串&#xff1a;<br /> 1、字符串长度小于等于127字节&#xff0c;只包含大小写字母&#xff0c;数字&#xff0c;下划线和偶数个双引号&#xff1b;<br /> 2、命令字之间以一个或多个下划线_进行分割&#xff1b;<br /> 3、可以通过两个双引号””来标识包含下划线_的命令字或空命令字&#xff08;仅包含两个双引号的命令字&#xff09;&#xff0c;双引号不会在命令字内部出现&#xff1b;</p> 
<p>请对指定索引的敏感字段进行加密&#xff0c;替换为******&#xff08;6个*&#xff09;&#xff0c;并删除命令字前后多余的下划线_。<br /> 如果无法找到指定索引的命令字&#xff0c;输出字符串ERROR。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入为两行&#xff0c;第一行为命令字索引K&#xff08;从0开始&#xff09;&#xff0c;第二行为命令字符串S。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出处理后的命令字符串&#xff0c;如果无法找到指定索引的命令字&#xff0c;输出字符串ERROR</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">1<br /> password__a12345678_timeout_100</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">password_******_timeout_100</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2<br /> aaa_password_&#34;a12_45678&#34;_timeout__100_&#34;&#34;_</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">aaa_password_******_timeout_100_&#34;&#34;</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>一开始我想构造一个正则只匹配“_”&#xff0c;而不会收到引号的影响&#xff0c;但是发现很难。</p> 
<p>于是&#xff0c;就想到之前有一题报文解压缩也是处理字符串&#xff0c;但是正则不好构造&#xff0c;后面使用栈结构来解决的。</p> 
<p>那么这题是否也可以用栈结构来解题呢&#xff1f;</p> 
<p>答案是可以的。逻辑如下&#xff1a;</p> 
<p>创建一个stack栈&#xff0c;用于接收输入字符串遍历出来的每一个字符</p> 
<ul><li>如果是&#39;_&#39;字符&#xff0c;则看看stack栈底是否为&#39;<span style="color:#fe2c24;">&#34;</span>&#39;&#xff0c;如果是&#xff0c;则说明当前‘_’字符是命令字的组成部分&#xff0c;而不是命令字分隔符&#xff0c;因此保留push进栈。如果不是&#xff0c;则说明当前&#39;_&#39;字符是分隔符&#xff0c;我们将stack栈中所有元素拼接为字符串保存进result&#xff0c;然后stack.length &#61; 0&#xff0c;将栈清空。</li><li>如果是&#39;<span style="color:#fe2c24;">&#34;</span>&#39;&#xff0c;则看看当前栈是否为空&#xff0c;若为空&#xff0c;则说明当前‘<span style="color:#fe2c24;">”</span>’是第一个引号&#xff0c;若不为空&#xff0c;则说明当前引号是第二个引号&#xff0c;此时我们需要将stack栈中所有元素拼接为字符串保存进result&#xff0c;然后stack.length &#61; 0&#xff0c;将栈清空。</li><li>如果既不是‘_’字符&#xff0c;也不是&#39;<span style="color:#fe2c24;">&#34;</span>&#39;字符&#xff0c;则是普通字符&#xff0c;直接push进stack</li></ul> 
<p>遍历完所有字符后&#xff0c;则判断stack是否为空&#xff0c;若不为空&#xff0c;则还需要将stack栈中所有元素拼接为字符串保存进result。</p> 
<p>当输入字符串,如<code>aaa_password_&#34;a12_45678&#34;_timeout__100_&#34;&#34;_ </code></p> 
<p>经过上面逻辑处理后&#xff0c;result内容如下</p> 
<p>[</p> 
<p>&#39;<span style="color:#fe2c24;"><code>aaa</code></span>&#39;,</p> 
<p>&#39;<span style="color:#fe2c24;"><code>password</code></span>&#39;,</p> 
<p>&#39;<span style="color:#fe2c24;"><code>&#34;a12_45678&#34;</code></span>&#39;<code>,</code></p> 
<p>&#39;<span style="color:#fe2c24;"><code>timeout</code></span>&#39;&#xff0c;</p> 
<p>&#39;&#39;&#xff0c;</p> 
<p>&#39;<span style="color:#fe2c24;">100</span>&#39;&#xff0c;</p> 
<p>&#39;<span style="color:#fe2c24;">&#34;&#34;</span>&#39;,</p> 
<p>&#39;&#39;</p> 
<p>]</p> 
<p>可以发现&#xff0c;result中存在不少空串&#xff0c;因此我们需要过滤掉空串</p> 
<p>[</p> 
<p>&#39;<span style="color:#fe2c24;"><code>aaa</code></span>&#39;,</p> 
<p>&#39;<span style="color:#fe2c24;"><code>password</code></span>&#39;,</p> 
<p>&#39;<span style="color:#fe2c24;"><code>&#34;a12_45678&#34;</code></span>&#39;<code>,</code></p> 
<p>&#39;<span style="color:#fe2c24;"><code>timeout</code></span>&#39;&#xff0c;</p> 
<p>&#39;<span style="color:#fe2c24;">100</span>&#39;&#xff0c;</p> 
<p>&#39;<span style="color:#fe2c24;">&#34;&#34;</span>&#39;</p> 
<p>]</p> 
<p>此时找到索引K对应的元素&#xff0c;将其替换为******</p> 
<p>[</p> 
<p>&#39;<span style="color:#fe2c24;"><code>aaa</code></span>&#39;,</p> 
<p>&#39;<span style="color:#fe2c24;"><code>password</code></span>&#39;,</p> 
<p>&#39;<span style="color:#a2e043;">******</span>&#39;<code>,</code></p> 
<p>&#39;<span style="color:#fe2c24;"><code>timeout</code></span>&#39;&#xff0c;</p> 
<p>&#39;<span style="color:#fe2c24;">100</span>&#39;&#xff0c;</p> 
<p>&#39;<span style="color:#fe2c24;">&#34;&#34;</span>&#39;</p> 
<p>]</p> 
<p>最后以‘_’连接result中的元素为字符串返回&#xff0c;就是题解</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const k &#61; parseInt(lines[0]);
    const s &#61; lines[1];

    console.log(encryptSensitive(s, k));

    lines.length &#61; 0;
  }
});

function encryptSensitive(s, k) {
  let stack &#61; [];
  let result &#61; [];

  for (let i &#61; 0; i &lt; s.length; i&#43;&#43;) {
    if (s[i] &#61;&#61;&#61; &#34;_&#34; &amp;&amp; stack[0] !&#61;&#61; &#39;&#34;&#39;) {
      result.push(stack.join(&#34;&#34;));
      stack.length &#61; 0;
    } else if (s[i] &#61;&#61;&#61; &#39;&#34;&#39; &amp;&amp; stack.length !&#61;&#61; 0) {
      result.push(stack.join(&#34;&#34;) &#43; &#39;&#34;&#39;);
      stack.length &#61; 0;
    } else {
      stack.push(s[i]);
    }
  }
  if (stack.length) result.push(stack.join(&#34;&#34;));

  result &#61; result.filter((ele) &#61;&gt; ele !&#61;&#61; &#34;&#34;);

  if (k &gt; result.length - 1) return &#34;ERROR&#34;;

  result[k] &#61; &#34;******&#34;;

  return result.join(&#34;_&#34;);
}</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int k &#61; Integer.parseInt(sc.nextLine());
    String s &#61; sc.nextLine();

    System.out.println(getResult(k, s));
  }

  public static String getResult(int k, String s) {
    StringBuilder stack &#61; new StringBuilder();
    LinkedList&lt;String&gt; result &#61; new LinkedList&lt;&gt;();

    for (int i &#61; 0; i &lt; s.length(); i&#43;&#43;) {
      char c &#61; s.charAt(i);

      if (c &#61;&#61; &#39;_&#39; &amp;&amp; (stack.length() &#61;&#61; 0 || stack.charAt(0) !&#61; &#39;&#34;&#39;)) {
        result.add(stack.toString());
        stack &#61; new StringBuilder();
      } else if (c &#61;&#61; &#39;&#34;&#39; &amp;&amp; stack.length() !&#61; 0) {
        stack.append(&#39;&#34;&#39;);
        result.add(stack.toString());
        stack &#61; new StringBuilder();
      } else {
        stack.append(c);
      }
    }

    if (stack.length() &gt; 0) result.add(stack.toString());

    List&lt;String&gt; ans &#61; result.stream().filter(str -&gt; !&#34;&#34;.equals(str)).collect(Collectors.toList());

    if (k &gt; ans.size() - 1) return &#34;ERROR&#34;;
    ans.set(k, &#34;******&#34;);

    StringJoiner sj &#61; new StringJoiner(&#34;_&#34;);
    for (String an : ans) sj.add(an);
    return sj.toString();
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
k &#61; int(input())
s &#61; input()


# 算法入口
def getResult(k, s):
    stack &#61; []
    res &#61; []

    for c in s:
        if c &#61;&#61; &#39;_&#39; and (len(stack) &#61;&#61; 0 or stack[0] !&#61; &#39;&#34;&#39;):
            res.append(&#34;&#34;.join(stack))
            stack &#61; []
        elif c &#61;&#61; &#39;&#34;&#39; and len(stack) !&#61; 0:
            stack.append(c)
            res.append(&#34;&#34;.join(stack))
            stack &#61; []
        else:
            stack.append(c)

    if len(stack) &gt; 0:
        res.append(&#34;&#34;.join(stack))

    ans &#61; list(filter(lambda x: x !&#61; &#34;&#34;, res))

    if k &gt; len(ans) - 1:
        return &#34;ERROR&#34;

    ans[k] &#61; &#34;******&#34;

    return &#34;_&#34;.join(ans)


# 算法调用
print(getResult(k, s))
</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>